package com.molichuxing.services.infrastructure.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.dto.TabDto;
import com.molichuxing.services.infrastructure.bean.DealerBean;
import com.molichuxing.services.infrastructure.dto.request.create.DeviceCreateDto;
import com.molichuxing.services.infrastructure.dto.request.modify.DeviceModifyDto;
import com.molichuxing.services.infrastructure.dto.response.DealerDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.molichuxing.services.infrastructure.bean.DeviceBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.DeviceMapper;
import com.molichuxing.services.infrastructure.dto.response.DeviceDto;
import com.molichuxing.services.infrastructure.service.DeviceService;

/**
 * <p>
 * 经销商安装的设备表 服务实现类
 * </p>
 *
 * @author zph
 * @since 2019-08-12
 */
@Service("deviceService")
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    DeviceMapper deviceMapper;

    /**
     * 通过id获取设备信息
     *
     * @param ids
     * @return
     * @throws Exception
     */
    @Override
    public List<DeviceDto> getDeviceListByIds(List<Integer> ids) throws Exception {
        List<DeviceBean> deviceBeanList = deviceMapper.selectBatchByIds(ids);
        return Convert.toDeviceDtoList(deviceBeanList);
    }

    /**
     * 通过cpu获取设备id
     *
     * @param cpus
     * @return
     * @throws Exception
     */
    @Override
    public List<Integer> getDeviceListByCpu(List<String> cpus) throws Exception {
        List<DeviceBean> deviceBeanList = deviceMapper.selectListByCpus(cpus);
        List<Integer> idList = new ArrayList<>();
        for (DeviceBean deviceBean : deviceBeanList) {
            Integer deviceId = deviceBean.getId();
            if (deviceId != null) {
                idList.add(deviceId);
            }
        }

        return idList;
    }

    /**
     * 根据cpu获取设备id
     *
     * @param cpu
     * @return
     */
    @Override
    public Integer getDeviceIdByCpu(String cpu) {
        DeviceBean deviceBean = deviceMapper.getByCpu(cpu);
        if (deviceBean == null) {
            return null;
        }
        return deviceBean.getId();
    }

    @Override
    public DeviceDto getDeviceByCpu(String cpu) {
        return Convert.toDeviceDto(deviceMapper.getByCpu(cpu));
    }

    @Override
    public Boolean createDevice(DeviceCreateDto createDto) {
        int result = deviceMapper.create(Convert.toDeviceBean(createDto));
        if (result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public Boolean modifyDevice(DeviceModifyDto modifyDto) {
        int result = deviceMapper.modifyDevice(Convert.toDeviceBean(modifyDto));
        if (result > 0) {
            return true;
        }
        return false;
    }


    @Override
    public DeviceDto getDeviceById(Integer id) {
        DeviceBean deviceBean = deviceMapper.getById(id);
        return Convert.toDeviceDto(deviceBean);
    }

    /**
     * 设备列表
     *
     * @param map
     * @param pageNum
     * @param pageSize
     * @return
     * @throws Exception
     */
    @Override
    public Paged<DeviceDto> getDevicePage(Map<String, Object> map, Integer pageNum, Integer pageSize) throws Exception {
        List<DeviceBean> deviceBeanList = deviceMapper.getPage(map, pageNum, pageSize);
        long pageCount = deviceMapper.getPageCount(map);
        List<DeviceDto> deviceBeanDtoList = Convert.toDeviceDtoList(deviceBeanList);
        return new Paged<DeviceDto>(deviceBeanDtoList, pageCount, pageSize);
    }

    /**
     * 经销商数量
     *
     * @return
     */
    @Override
    public Integer getCount(List<Integer> deviceIdList,LocalDateTime startHeartTime, LocalDateTime endHeartTime) {
        return deviceMapper.getCount(deviceIdList,startHeartTime, endHeartTime);
    }

}
